package com.qianbao.controller.v1.user;


import com.qianbao.components.excepion.CommonException;
import com.qianbao.components.excepion.ExceptionEnum;
import com.qianbao.components.svresut.Result;
import com.qianbao.components.svversion.ApiVersion;
import com.qianbao.domain.user.ModifyPasswordEntity;
import com.qianbao.domain.user.UserEntity;
import com.qianbao.service.user.SVUserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.io.Serializable;
import java.util.Collections;
import java.util.HashMap;


@Controller
@RequestMapping("usercenter/{version}/")
@ApiVersion(1)
public class UserAccountController {

    @Autowired
    private SVUserService service;


    /**
     * 注册
     * @param entity
     * @return
     */
    @RequestMapping("/register")
    @ResponseBody
    public Result registerAccount(@Valid UserEntity entity){

        try {

            int row = service.saveUser(entity);

            Result result ;
            if (row >= 1){
                result = Result.success(ExceptionEnum.Register_Success.getCode(),ExceptionEnum.Register_Success.getMessage(), Collections.emptyMap());
            }else {
                result = Result.fail(ExceptionEnum.Register_fail.getCode(),ExceptionEnum.Register_fail.getMessage());
            }

            return result;
        } catch (CommonException e) {
            return Result.fail(e.getCode(),e.getMessage());
        }

    }


    /**
     * 登录接口
     * @param entity
     * @return
     */
    @RequestMapping("/login")
    @ResponseBody
    public Result loginAccount(@Valid UserEntity entity){

        try {
            UserEntity user = service.loginAccount(entity);
            // 返回状态
            return Result.success(
                    ExceptionEnum.Login_Success.getCode(),
                    ExceptionEnum.Login_Success.getMessage(),
                    user);
        }catch (CommonException e){
            return Result.fail(e.getCode(),e.getMessage());
        }

    }


    /**
     * 未登录的跳转链接
     * @return
     */
    @RequestMapping("/notlogin")
    @ResponseBody
    public Result notLoginAccount(){
        return Result.fail(ExceptionEnum.Token_fail.getCode(),ExceptionEnum.Token_fail.getMessage());
    }

    /**
     * 没有权限的跳转链接
     * @return
     */
    @RequestMapping("/notAuth")
    @ResponseBody
    public Result notAuth(){
        return Result.fail(ExceptionEnum.Auth_fail.getCode(),ExceptionEnum.Auth_fail.getMessage());
    }


    /**
     * 检查手机号是否注册
     * @return
     */
    @RequestMapping("/check-phone-register")
    @ResponseBody
    public Result checkAccount(String phone){

        try {

            boolean user = service.findUserByPhone(phone);

            HashMap<String,String> map = new HashMap<>();

            if (user){

                map.put("success","true");

                return Result.success(map);

            }else {

                map.put("success","false");

                return Result.success(map);
            }
        } catch (CommonException e) {
            e.printStackTrace();
            return Result.fail(e.getCode(),e.getMessage());
        }

    }


    /**
     * 忘记密码，用户未登录的情况
     * @return
     */

    @RequestMapping("/forget-password")
    @ResponseBody
    public Result forgetPassword(@Valid UserEntity userEntity){

        try {
            boolean success = service.updateUserPassword(userEntity);

            if (success){
                return Result.success(
                        ExceptionEnum.Update_password_success.getCode(),
                        ExceptionEnum.Update_password_success.getMessage(),
                        Collections.emptyMap());
            }else {

                return Result.fail(
                        ExceptionEnum.Update_password_fail.getCode(),
                        ExceptionEnum.Update_password_fail.getMessage()
                        );

            }

        } catch (CommonException e) {
            e.printStackTrace();
            return Result.fail(e.getCode(),e.getMessage());
        }

    }


    /**
     * 修改登录密码，用户已经登录的情况下
     * @return
     */
    @ResponseBody
    @RequestMapping("/modify-password")
    public Result modifyPassword(@Valid ModifyPasswordEntity entity){

        try {
            boolean flag = service.modifyUserPassword(entity);

            if (flag){
                return Result.success(
                        ExceptionEnum.Modify_password_Success.getCode(),
                        ExceptionEnum.Modify_password_Success.getMessage(),
                        Collections.emptyMap());
            }else {
                return Result.fail(
                        ExceptionEnum.Modify_password_Fail.getCode(),
                        ExceptionEnum.Modify_password_Fail.getMessage()
                );
            }

        } catch (CommonException e) {
            return Result.fail(e.getCode(),e.getMessage());
        }

    }





    /**
     * 退出登录
     * @return
     */
    @RequestMapping("/logout")
    @ResponseBody
    public Result logoutAccount(){

        try {
            service.logoutAccount();
            return Result.success(
                    ExceptionEnum.Login_out_Success.getCode(),
                    ExceptionEnum.Login_out_Success.getMessage(),
                    Collections.emptyMap()
            );
        } catch (CommonException e) {
            return Result.fail(e.getCode(),e.getMessage());
        }

    }



}
